package cn.imvc.app.jobs;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.imvc.app.domains.OrderState;
import cn.imvc.app.entities.Orders;
import cn.imvc.app.models.OrdersModel;
import freemarker.template.SimpleDate;

@DisallowConcurrentExecution
public class AutomaticCancelOrderQuarz {
	@Autowired
	private OrdersModel ordersModel;

	/**
	 * 自动取消订单(30分钟)
	 * 
	 * @param context
	 */
	public void automaticCancelOrder(JobExecutionContext context) {
		Calendar calendar1 = Calendar.getInstance();
		calendar1.add(Calendar.MINUTE, -30);
		Date date1 = calendar1.getTime();
		/**
		 * 创建订单状态 30分钟取消
		 */
		List<Orders> ordersList = ordersModel.findByStateAndCreatedAtLessThan(OrderState.Create.getValue(), date1);
		for (Orders order : ordersList) {
			ordersModel.updateSystemCancelOrder(order);
		}

		/**
		 * 支付中状态 60分钟取消
		 */
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.MINUTE, -60);
		Date date2 = calendar2.getTime();
		
		List<Orders> ordersList1 = ordersModel.findByStateAndCreatedAtLessThan(OrderState.Payment.getValue(), date2);
		for (Orders order : ordersList1) {
			ordersModel.updateSystemCancelOrder(order);
		}
		System.out.println("订单取消任务完成！！");
	}
}
